package com.zhangsheng.service.impl;

import com.zhangsheng.entity.model.Order;
import com.zhangsheng.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * redis操作实现类
 * Created by macro on 2020/3/3.
 */
@Service
public class RedisServiceImpl implements RedisService {

    public static final String REDIS_ORDER_KEY = "order:orderMap";

    @Autowired
    public RedisTemplate<String, Object> redisTemplate;

    @Override
    public void createOrderByRedis(Order order) {
        String orderNo = getRandomOrderNo();
        redisTemplate.opsForHash().put(REDIS_ORDER_KEY, orderNo, 0);
    }

    public String getRandomOrderNo(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowDate = sdf.format(new Date());
        return UUID.randomUUID() + nowDate;
    }

    public void generateOrderToken(String orderNo){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowDate = sdf.format(new Date());
        redisTemplate.opsForSet().add(orderNo , UUID.randomUUID() + nowDate);
    }

}
